<?php
class omeanalysts_mdl_ome_platformordercheckSum extends dbeav_model{

    //var $has_export_cnf = true;

    var $export_name = '平台订单核对总表';

    public function table_name($real=false){
        $table_name = 'ome_delivery';
        if($real){
            return kernel::database()->prefix.$table_name;
        }else{
            return $table_name;
        }
    }

    public function count($filter=null){

        $where = $this->_filter($filter); 
   $sql = "SELECT t.`name` shop_name,t.AXCustomer,t.customername,t.bn,t.product_name,sum(t.sumqty) sumqty,sum(t.ofqty) ofqty,sum(t.deliveryqty) deliveryqty,sum(t.returnqty) returnqty,sum(t.onlyreturnqty) onlyreturnqty from (
                  select * from (select f.`name`,f.AXCustomer,f.customername,items.bn,items.`name` product_name,sum(items.nums)sumqty,0 ofqty,0 deliveryqty,0 returnqty,0 onlyreturnqty from sdb_ome_orders os 
                  inner join sdb_ome_order_items items on os.order_id=items.order_id
                  inner join sdb_ome_shop f on os.shop_id=f.shop_id
                  where ".$where." and items.`delete`='false'
                  group by f.`name`,f.AXCustomer,f.customername,items.bn,items.`name`)t1
                  union
                  select * from (
                  SELECT f.`name`,f.AXCustomer,f.customername,items.bn,items.`name` product_name,0 sumqty,sum(case when e.is_ship='true' then its.number else 0 end) ofqty,sum(case when e.status=3 then its.number else 0 end) deliveryqty,0 returnqty,0 onlyreturnqty 
                  from  sdb_wms_delivery e 
                  inner JOIN sdb_ome_delivery od on ( e.outer_delivery_bn = od.delivery_bn )
                  inner JOIN sdb_ome_delivery_order odo on ( od.delivery_id = odo.delivery_id )
                  inner JOIN sdb_ome_orders os on (odo.order_id = os.order_id)
                  inner join sdb_ome_order_items items on os.order_id=items.order_id
                  inner join sdb_wms_delivery_items its on e.delivery_id=its.delivery_id and items.bn=its.bn
                  inner join sdb_ome_shop f on os.shop_id=f.shop_id
                  where ".$where."
                  group by f.`name`,f.AXCustomer,f.customername,items.bn,items.`name`)t2
                  union
                  SELECT * from (
                  select f.`name`,f.AXCustomer,f.customername,items.bn,items.product_name,0 sumqty,0 ofqty,0 deliveryqty,sum(case when rp.return_type='return' then items.num else 0 end) returnqty,
    sum(case when rp.return_type='onlyreturn' then items.num  else 0 end) onlyreturnqty from sdb_ome_orders os 
                  inner join sdb_ome_reship rp on os.order_id=rp.order_id
                  inner join sdb_ome_reship_items items on rp.reship_id=items.reship_id
                  inner join sdb_ome_shop f on os.shop_id=f.shop_id
                  where ".$where."
                  group by f.`name`,f.AXCustomer,f.customername,items.bn,items.product_name)t3)t
                  group by t.`name`,t.AXCustomer,t.customername,t.bn,t.product_name";
        $row = $this->db->select($sql);

        return count($row);
    }

    public function getlist($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null){
  

        if(!$filter['time_from'] && !$filter['time_to']){
            return false;
        }else{
            $where = $this->_filter($filter); 
             $sql = "SELECT t.`name` shop_name,t.AXCustomer,t.customername,t.bn,t.product_name,sum(t.sumqty) sumqty,sum(t.ofqty) ofqty,sum(t.deliveryqty) deliveryqty,sum(t.returnqty) returnqty,sum(t.onlyreturnqty) onlyreturnqty from (
                  select * from (select f.`name`,f.AXCustomer,f.customername,items.bn,items.`name` product_name,sum(items.nums)sumqty,0 ofqty,0 deliveryqty,0 returnqty,0 onlyreturnqty from sdb_ome_orders os 
                  inner join sdb_ome_order_items items on os.order_id=items.order_id
                  inner join sdb_ome_shop f on os.shop_id=f.shop_id
                  where ".$where." and items.`delete`='false'
                  group by f.`name`,f.AXCustomer,f.customername,items.bn,items.`name`)t1
                  union
                  select * from (
                  SELECT f.`name`,f.AXCustomer,f.customername,items.bn,items.`name` product_name,0 sumqty,sum(case when e.is_ship='true' then its.number else 0 end) ofqty,sum(case when e.status=3 then its.number else 0 end) deliveryqty,0 returnqty,0 onlyreturnqty 
                  from  sdb_wms_delivery e 
                  inner JOIN sdb_ome_delivery od on ( e.outer_delivery_bn = od.delivery_bn )
                  inner JOIN sdb_ome_delivery_order odo on ( od.delivery_id = odo.delivery_id )
                  inner JOIN sdb_ome_orders os on (odo.order_id = os.order_id)
                  inner join sdb_ome_order_items items on os.order_id=items.order_id
                  inner join sdb_wms_delivery_items its on e.delivery_id=its.delivery_id and items.bn=its.bn
                  inner join sdb_ome_shop f on os.shop_id=f.shop_id
                  where ".$where."
                  group by f.`name`,f.AXCustomer,f.customername,items.bn,items.`name`)t2
                  union
                  SELECT * from (
                  select f.`name`,f.AXCustomer,f.customername,items.bn,items.product_name,0 sumqty,0 ofqty,0 deliveryqty,sum(case when rp.return_type='return' then items.num else 0 end) returnqty,
    sum(case when rp.return_type='onlyreturn' then items.num  else 0 end) onlyreturnqty from sdb_ome_orders os 
                  inner join sdb_ome_reship rp on os.order_id=rp.order_id
                  inner join sdb_ome_reship_items items on rp.reship_id=items.reship_id
                  inner join sdb_ome_shop f on os.shop_id=f.shop_id
                  where ".$where."
                  group by f.`name`,f.AXCustomer,f.customername,items.bn,items.product_name)t3)t
                  group by t.`name`,t.AXCustomer,t.customername,t.bn,t.product_name";
            $rows = $this->db->selectLimit($sql,$limit,$offset);
            return $rows;
        }
        
    }
    
    public function searchOptions(){
        $parentOptions = parent::searchOptions();
        $childOptions = array();
        return $Options = array_merge($parentOptions,$childOptions);
    }

    public function _filter($filter,$tableAlias=null,$baseWhere=null)
    {
        $where = array(1);
       if(empty($filter['time_from']))
        {
          $where[]=' 1=2 ';
        }
        if(isset($filter['time_from']) && $filter['time_from']){
            $where[] = ' os.createtime >='.strtotime($filter['time_from']);
        }
        if(isset($filter['time_to']) && $filter['time_to']){
            $where[] = ' os.createtime <'.(strtotime($filter['time_to'])+86399);
        }
        if(isset($filter['AXCustomer']) && $filter['AXCustomer']){
            $where[]= ' f.AXCustomer  =\''.addslashes($filter['AXCustomer']).'\'';

        }  
        if(isset($filter['axcustomercode']) && $filter['axcustomercode']){
            $where[]= ' f.AXCustomer  =\''.addslashes($filter['axcustomercode']).'\'';

        }

        return implode($where,' AND ');
    }

    public function get_schema()
    {
        $schema = array (
            'columns' => array (
                'shop_name' =>
                array (
                  'type' => 'varchar(255)',
                  'required' => true,
                  'label' => '店铺名称',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '200',
                ),
                'AXCustomer' =>
                array (
                  'type' => 'varchar(255)',
                  'required' => true,
                  'label' => '客户编码',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ),
                'customername' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => '客户名称',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '200',
                ),
                'bn' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => '物料编码',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '100',
                ),
                'product_name' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => '物料名称',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '150',
                ),
                'sumqty' =>
                array (
                  'type' => 'number',
                  'label' => '总单数',
                  'required' => true,
                  'default' => 0,
                  'editable' => false,
                  'width' => '80',
                ),
                'ofqty' =>
                array (
                  'type' => 'number',
                  'label' => '已备货',
                  'required' => true,
                  'default' => 0,
                  'editable' => false,
                  'width' => '80',
                ),
                'deliveryqty' =>
                array (
                  'type' => 'number',
                  'label' => '已发货',
                  'required' => true,
                  'default' => 0,
                  'editable' => false,
                  'width' => '80',
                ),
                'returnqty' =>
                array (
                  'type' => 'number',
                  'label' => '退款退货',
                  'required' => true,
                  'default' => 0,
                  'editable' => false,
                  'width' => '80',
                ),
                'onlyreturnqty' =>
                array (
                  'type' => 'number',
                  'label' => '仅退货',
                  'required' => true,
                  'default' => 0,
                  'editable' => false,
                  'width' => '80',
                ),
            ),
            'idColumn' => 'name',
            'in_list' => array (
                0 => 'shop_name',
                1 => 'AXCustomer',
                2 => 'customername',
                3 => 'bn',
                4 => 'product_name',
                5 => 'sumqty',
                6 => 'ofqty',
                7 => 'deliveryqty',
                8 => 'returnqty',
                9 => 'onlyreturnqty',
            ),
            'default_in_list' => array (
                0 => 'shop_name',
                1 => 'AXCustomer',
                2 => 'customername',
                3 => 'bn',
                4 => 'product_name',
                5 => 'sumqty',
                6 => 'ofqty',
                7 => 'deliveryqty',
                8 => 'returnqty',
                9 => 'onlyreturnqty',
            ),

        );
        return $schema;
    }

    /**
     * 获得日志类型(non-PHPdoc)
     * @see dbeav_model::getLogType()
     */
    public function getLogType($logParams) {
        $type = $logParams['type'];
        $logType = 'none';
        if ($type == 'export') {
            $logType = $this->exportLogType($logParams);
        }
        elseif ($type == 'import') {
            $logType = $this->importLogType($logParams);
        }
        return $logType;
    }
    /**
     * 导出日志类型
     * @param Array $logParams 日志参数
     */
    public function exportLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_export';
        return $type;
    }
    /**
     * 导入操作日志类型
     * @param Array $logParams 日志参数
     */
    public function importLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_import';
        return $type;
    }

    function io_title( $filter, $ioType='csv' )
    {

        switch( $filter ){
            case 'delivery':
                $this->oSchema['csv'][$filter] = array(
                    '*:店铺名称' => 'shop_name',
                    '*:客户编码' => 'AXCustomer',
                    '*:客户名称' => 'customername',
                    '*:物料' => 'bn',
                    '*:物料名称' => 'product_name',
                    '*:总单数' => 'sumqty',
                    '*:已备货' => 'ofqty',
                    '*:已发货' => 'deliveryqty',
                    '*:退款退货' => 'returnqty',
                    '*:仅退货' => 'onlyreturnqty',
                );
                break;
        }
        $this->ioTitle[$ioType][$filter] = array_keys( $this->oSchema[$ioType][$filter] );
        return $this->ioTitle[$ioType][$filter];
    }

    //根据查询条件获取导出数据
    public function getExportDataByCustom($fields, $filter, $has_detail, $curr_sheet, $start, $end)
    {
          //根据选择的字段定义导出的第一行标题
          if($curr_sheet == 1){

              $title = array();
              foreach($this->io_title('delivery') as $k => $v ){
                  $title[] = mb_convert_encoding($v, 'GBK', 'UTF-8');
              }

              $data['content']['main'][0] = '"'.implode('","',$title).'"';
          }

          $where = $this->_filter($filter);
              $sql = "SELECT t.`name` shop_name,t.AXCustomer,t.customername,t.bn,t.product_name,sum(t.sumqty) sumqty,sum(t.ofqty) ofqty,sum(t.deliveryqty) deliveryqty,sum(t.returnqty) returnqty,sum(t.onlyreturnqty) onlyreturnqty from (
                  select * from (select f.`name`,f.AXCustomer,f.customername,items.bn,items.`name` product_name,sum(items.nums)sumqty,0 ofqty,0 deliveryqty,0 returnqty,0 onlyreturnqty from sdb_ome_orders os 
                  inner join sdb_ome_order_items items on os.order_id=items.order_id
                  inner join sdb_ome_shop f on os.shop_id=f.shop_id
                  where ".$where." and items.`delete`='false'
                  group by f.`name`,f.AXCustomer,f.customername,items.bn,items.`name`)t1
                  union
                  select * from (
                  SELECT f.`name`,f.AXCustomer,f.customername,items.bn,items.`name` product_name,0 sumqty,sum(case when e.is_ship='true' then its.number else 0 end) ofqty,sum(case when e.status=3 then its.number else 0 end) deliveryqty,0 returnqty,0 onlyreturnqty 
                  from  sdb_wms_delivery e 
                  inner JOIN sdb_ome_delivery od on ( e.outer_delivery_bn = od.delivery_bn )
                  inner JOIN sdb_ome_delivery_order odo on ( od.delivery_id = odo.delivery_id )
                  inner JOIN sdb_ome_orders os on (odo.order_id = os.order_id)
                  inner join sdb_ome_order_items items on os.order_id=items.order_id
                  inner join sdb_wms_delivery_items its on e.delivery_id=its.delivery_id and items.bn=its.bn
                  inner join sdb_ome_shop f on os.shop_id=f.shop_id
                  where ".$where."
                  group by f.`name`,f.AXCustomer,f.customername,items.bn,items.`name`)t2
                  union
                  SELECT * from (
                  select f.`name`,f.AXCustomer,f.customername,items.bn,items.product_name,0 sumqty,0 ofqty,0 deliveryqty,sum(case when rp.return_type='return' then items.num else 0 end) returnqty,
    sum(case when rp.return_type='onlyreturn' then items.num  else 0 end) onlyreturnqty from sdb_ome_orders os 
                  inner join sdb_ome_reship rp on os.order_id=rp.order_id
                  inner join sdb_ome_reship_items items on rp.reship_id=items.reship_id
                  inner join sdb_ome_shop f on os.shop_id=f.shop_id
                  where ".$where."
                  group by f.`name`,f.AXCustomer,f.customername,items.bn,items.product_name)t3)t
                  group by t.`name`,t.AXCustomer,t.customername,t.bn,t.product_name";
            $omedelivery = $this->db->selectLimit($sql,$end,$start);
            
            foreach ($omedelivery as $k => $aFilter) {
                $exptmp_data = array();
                foreach ($aFilter as $col => $val) {
                    $aFilter[$col] = mb_convert_encoding($aFilter[$col], 'GBK', 'UTF-8');
                    $exptmp_data[] = $aFilter[$col];
                }

                $data['content']['main'][] = implode(',', $exptmp_data);
            }
            return $data;
        
    }
}
